<?php
class Admin_MenuController extends Zend_Controller_Action{

	public function init(){
		parent::init();
	}

	protected function buildTableMenus() {
		$tableHtml = "<table cellpadding = '0' cellspacing = '0' class = 'table-data' border='0'>
						  <thead>
						  <tr>
						    <th>Menu name</th>
						    <th>Description</th>
						    <th>Menu image</th>
						    <th>Update</th>
						    <th>Delete</th>
						  </tr>
						  </thead>
					  ";
		$menuMapper = new Front_Model_MenuMapper();
		$entries = $menuMapper->fetchAll();

		if (isset($entries)) {
			$url = new Zend_View_Helper_Url();

			foreach ($entries as $row) {
				$rowStr = '<tr>';
				// menu name
				$menuname = $row->_menuname;
				if (!isset($menuname)) {
					$menuname = '';
				}
				$rowStr = $rowStr .'<td>'.$menuname.'</td>';
				// description
				$description = $row->_description;
				if (!isset($description)) {
					$description = '';
				}
				$rowStr = $rowStr .'<td>'.$description.'</td>';
				// menu image
				$menuimage = $row->_menuimage;
				if (!isset($menuimage)) {
					$menuimage = '';
				}
				$rowStr = $rowStr .'<td>'.$menuimage.'</td>';
				// update link
				$updatelink = $url->url(array('module'=>'admin', 'controller'=>'menu','action'=>'updatemenu','id'=>$row->_mid));
				if (!isset($updatelink)) {
					$updatelink = '';
				}
				$rowStr = $rowStr ."<td><a href = '".$updatelink."'>Update</a>".'</td>';
				// delete link
				$deletelink = $url->url(array('module'=>'admin', 'controller'=>'menu','action'=>'deletemenu','id'=>$row->_mid));
				if (!isset($deletelink)) {
					$deletelink = '';
				}
				$rowStr = $rowStr ."<td><a href = '".$deletelink."'>Delete</a>".'</td>';
				$rowStr = $rowStr.'</tr>';
				$tableHtml = $tableHtml.$rowStr;
			}
		}

		$tableHtml = $tableHtml .'</table>';

		$this->view->menus = $tableHtml;

	}


	public function indexAction() {
		$this->_forward('addmenu','menu','admin');

	}

	public function addmenuAction() {
		$this->buildTableMenus();
		$formMenu   = new Admin_Form_Menu('create');
		$formMenu->setMethod('post');
		$this->view->form = $formMenu;
		if ($this->getRequest()->isPost() ) {
			$formData = $this->getRequest()->getPost();
			if ($formMenu->isValid($formData)) {
				$menuname = $this->getRequest()->getParam('menuname');
				$menudescription = $this->getRequest()->getParam('description');
				$zendTranfers = new Zend_File_Transfer_Adapter_Http();

				$menuInfo = new Front_Model_Menu();
				$menuInfo->_menuname = $menuname;
				$menuInfo->_description = $menudescription;

				$menuMapper = new Front_Model_MenuMapper();
				$mid = $menuMapper->save($menuInfo);

				if ($zendTranfers->isValid() && isset($mid)) {
						
					$menuInfo->_menuimage = $zendTranfers->getFileName('menuimage',false);
					$newName = $menuInfo->_menuimage;
					$ex = end(explode(".", $newName));
					$newName = $mid.'.'.$ex;
						
					$menuInfo->_mid = $mid;
					$menuInfo->_menuimage = $newName;
					$menuMapper->save($menuInfo);
					$zendTranfers->addFilter('Rename',
					array('target' => PUBLIC_PATH .'/Images/'.$newName,
						  'overwrite' => true));
					try {
						// upload received file(s)
						$zendTranfers->receive();
					} catch (Zend_File_Transfer_Exception $e) {
						echo $e->getMessage();
						return;
					}
				}

				My_Controller_Plugin_Acl::goPage($this->getResponse(), 'admin', 'menu', 'index');
			}

		}
	}

	public function updatemenuAction() {
		$this->buildTableMenus();
		$mid = $this->getRequest()->getParam('id');
		$menuMapper = new Front_Model_MenuMapper();
		$menuInfo = new Front_Model_Menu();
		$hasMenu = $menuMapper->find($mid, $menuInfo);
		if (isset($hasMenu) && isset($menuInfo)) {
			// Create form
			$formMenu   = new Admin_Form_Menu('update');
			$formMenu->setMethod('post');
			$formMenu->populateData($menuInfo);

			// Bind data
			$this->view->form = $formMenu;

			if ($this->getRequest()->isPost()) {
				$formData = $this->getRequest()->getPost();
				if ($formMenu->isValid($formData)) {
					$menuname = $this->getRequest()->getParam('menuname');
					$menudescription = $this->getRequest()->getParam('description');

					$menuInfo->_mid = $mid;
					$menuInfo->_menuname = $menuname;
					$menuInfo->_description = $menudescription;

					$zendTranfers = new Zend_File_Transfer_Adapter_Http();

					$menuMapper = new Front_Model_MenuMapper();
					$menuMapper->save($menuInfo);
						
					$filePath = $zendTranfers->getFileName('menuimage');
					if (isset($filePath) && $zendTranfers->isValid()) {
						$newName = $menuInfo->_menuimage;
						//$ex = end(explode(".", $newName));
						//$newName = $mid.'.'.$ex;
						$zendTranfers->addFilter('Rename',
						array('target' => PUBLIC_PATH .'/Images/'.$newName,
						  'overwrite' => true));
						try {
							// upload received file(s)
							$zendTranfers->receive();
						} catch (Zend_File_Transfer_Exception $e) {
							echo $e->getMessage();
							return;
						}
					}
						
					My_Controller_Plugin_Acl::goPage($this->getResponse(), 'admin', 'menu', 'index');
				}

			}
		}
	}

	public function deletemenuAction() {
		$mid = $this->getRequest()->getParam('id');
		$menuMapper = new Front_Model_MenuMapper();
		if (isset($mid)) {
			$menuMapper->delete($mid);
			My_Controller_Plugin_Acl::goPage($this->getResponse(), 'admin', 'menu', 'index');
		}
	}
}